import sys
from ctypes import *
from datetime import datetime
import os

if __name__ == "__main__":
    # 通过 ctypes.CDLL 去加载动态库；
    list_of_arguments = sys.argv
    print(list_of_arguments, type(list_of_arguments))
    cwd_path = os.getcwd()
    dll_path = '\\FL.Projects.DataDownloadDll.dll'
    print("work path", cwd_path)
    if os.path.isfile(cwd_path + dll_path):
        down_lib = CDLL(cwd_path + dll_path)
    else:
        cwd_path += '\\datadown'
        down_lib = CDLL(cwd_path + dll_path)

    # //开始时间/结束时间/  路径/仪表号/通道号/跑道/ 数据类型-0 原始数据-1 行车数据/ 起始点/结束点
    # public static extern void DataDownloadSync(long begin, long end, string path, int dev, int chn,
    # char c_track = '\0', int type = 0, int startPos = -1, int endPos = -1);//同步

    # f = getattr(down_lib, 'DataDownloadSync', None)
    # print(f)

    # 直接传递一个字符串，返回一个地址
    # path = c_wchar_p(path)
    # path = c_char_p(b'E:/test/')
    # print(path.value)
    # path = b'E:/test/'
    path = c_char_p(bytes(cwd_path + '\\data\\', encoding='utf-8'))
    print(path)

    # print(ctypes.c_char(b"a"))  # c_char(b'a')

    # 相当于告诉 ctypes，在解析 test2 函数返回值的时候
    # 请按照 c_float 进行解析，然后拿到的就是 Python 的 float
    # lib.test2.restype = c_float

    # 同理对于 unicode 也是一样的
    # 如果不指定类型，得到的依旧是一个整数
    # lib.test2.restype = c_wchar_p

    # sta_time = '2021-11-30 13:23:00'
    # end_time = '2021-11-30 13:26:00'   # 左闭  右开   26 - 23 = 3  即 三分钟的 数据 23 24 25
    # time_format = '%Y-%m-%d %H:%M:%S'
    # sta = datetime.strptime(sta_time, time_format).timestamp() * 1000
    # end = datetime.strptime(end_time, time_format).timestamp() * 1000
    if len(list_of_arguments) == 1:
        time_now = datetime.now()
        sta = int((time_now.timestamp() - 120) * 1000)
        end = int(time_now.timestamp() * 1000)
    else:
        sta = int(list_of_arguments[1])
        end = int(list_of_arguments[2])
    print(sta, end)
    sta = c_longlong(int(sta))
    end = c_longlong(int(end))
    print(sta.value, end.value)
    down_lib.DataDownloadSync(sta, end, path, 0, -1, 0, 1, -1, -1)


